package uk.ac.sanger.solr;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

import org.biodas.jdas.schema.registrycoordinates.DASCOORDINATESYSTEM;
import org.biodas.jdas.schema.sources.CAPABILITY;
import org.biodas.jdas.schema.sources.COORDINATES;
import org.biodas.jdas.schema.sources.PROP;
import org.biodas.jdas.schema.sources.SOURCE;
import org.biodas.jdas.schema.sources.SOURCES;
import org.biodas.jdas.schema.sources.VERSION;
import org.springframework.web.client.RestTemplate;

public class CreateSolrCoordinatesIndexes {

	String nl;
	CreateSolrIndexerUtil util;
		
	public static void main(String [] args){
		String homeDir=args[0];
		
		CreateSolrCoordinatesIndexes solr=new CreateSolrCoordinatesIndexes();
		solr.creatSolrIndexes(homeDir);
	}
	
	
	public void creatSolrIndexes(String homeDir){
		
//		if(!homeDir.endsWith("/")){
//			homeDir+=homeDir+"/";
//		}
		util=new CreateSolrIndexerUtil();
		nl=util.getLineSeperator();
		RestTemplate temp=new RestTemplate();
		String coordinatesUrl="http://www.dasregistry.org/" + "das/coordinatesystem";
		DASCOORDINATESYSTEM coordinateSystems=temp.getForObject(coordinatesUrl, DASCOORDINATESYSTEM.class);
		
		File file=new File(homeDir+"solrCoordinates.xml");
                System.out.println("creating file "+file.getAbsolutePath());
		OutputStreamWriter out = null;
		try {
			 out=new OutputStreamWriter(new FileOutputStream(file),  "UTF-8");
		} catch (UnsupportedEncodingException e1) {
			//  Auto-generated catch block
			e1.printStackTrace();
		} catch (FileNotFoundException e1) {
			//  Auto-generated catch block
			e1.printStackTrace();
		}
		System.out.println("writing coordinates for solrIndexer at "+file.getAbsolutePath());
		
		PrintWriter writer = null;
			writer = new PrintWriter(out);
		//NEED to add code to remove <a href from any fields!!!!
		writer.write("<add overwrite=\"true\" commitWithin=\"10000\">"+nl);
		for(COORDINATES coordinate:coordinateSystems.getCOORDINATES()){
			
			this.writeSolrCoord(coordinate, writer);
		}
		writer.write("</add>");
		writer.flush();
		writer.close();
		
	}


	private void writeSolrCoord(COORDINATES coord, PrintWriter out) {
		out.write("<doc>"+nl);
		createField(out, "id", coord.getUri().substring(coord.getUri().lastIndexOf("CS_DS"), coord.getUri().length()));
		createField(out, "website", "http://www.dasregistry.org");
                createField(out,"domain","Dasregistry");
		createField(out, "domain_url", coord.getUri());
		createField(out, "content",coord.getContent());
		createField(out, "taxid",coord.getTaxid());
		createField(out, "authority",coord.getAuthority());
		createField(out, "version",coord.getVersion());
		createField(out, "source", coord.getSource());
		createField(out, "test_range", coord.getTestRange());
		out.write("</doc>"+nl);
	}


	/**
	 * @param out
	 * @param string
	 * @param string2
	 */
	private void createField(PrintWriter out, String name, String content) {
		util.createField(out, name, content);
		
	}
	
	
	
}
